#!/usr/bin/env python
# import sip
# sip.setapi('QVariant', 2)

from PySide.QtCore import *
from PySide.QtGui import *

import datahandler
import commonfunctions

class CreateExaminerTab(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)

        self.data = datahandler.DataHandler()
        self.common = commonfunctions.CommonFunctions()
        self.parent = parent
        
        self.addExaminerAction = QAction(QIcon(QDir.currentPath() + "/images/list-add.png"), "Add New Examiner", self)
        self.addCaseExaminerAction = QAction(QIcon(QDir.currentPath() + "/images/go-bottom.png"), "Add Selected Examiner to Case", self)
        self.delCaseExaminerAction = QAction(QIcon(QDir.currentPath() + "/images/go-top.png"), "Remove Selected Examiner from Case", self)
        self.examinerUpAction = QAction(QIcon(QDir.currentPath() + "/images/go-up.png"), "Move Selected Examiner Up", self)
        self.examinerDownAction = QAction(QIcon(QDir.currentPath() + "/images/go-down.png"), "Move Selected Examiner Down", self)

        self.examinertb = QToolBar()
        self.examinertb.addAction(self.addExaminerAction)
        self.examinertb.addSeparator()
        self.examinertb.addAction(self.addCaseExaminerAction)
        self.examinertb.addAction(self.delCaseExaminerAction)
        self.examinertb.addAction(self.examinerDownAction)
        self.examinertb.addAction(self.examinerUpAction)
        self.examinertb.setStyleSheet("QToolBar { border: 0px }")

        for tmpaction in [self.addCaseExaminerAction, self.delCaseExaminerAction, self.examinerDownAction, self.examinerUpAction]:
            tmpaction.setEnabled(False)

        self.examinerList = QListWidget()
        self.caseexaminerList = QListWidget()
        self.caseLabel = QLabel("Ordered Case Examiners")
        self.examLabel = QLabel("Available Examiners")
        for tmplist in [self.examinerList, self.caseexaminerList]:
            tmplist.setSelectionMode(QAbstractItemView.SingleSelection)
            tmplist.setAlternatingRowColors(True)
            tmplist.setFrameStyle(QFrame.Panel)

        # connect actions
        self.addExaminerAction.triggered.connect(self.addExaminer)
        self.addCaseExaminerAction.triggered.connect(self.addCaseExaminer)
        self.delCaseExaminerAction.triggered.connect(self.delCaseExaminer)
        self.examinerDownAction.triggered.connect(self.examinerDown)
        self.examinerUpAction.triggered.connect(self.examinerUp)
        self.examinerList.itemClicked.connect(self.updateActions)
        self.caseexaminerList.itemClicked.connect(self.updateActions)

        self.examBox = QVBoxLayout()
        self.mainBox = QHBoxLayout()
        self.examBox.addWidget(self.examinertb)
        self.examBox.addWidget(self.examLabel)
        self.examBox.addWidget(self.examinerList)
        self.examBox.addWidget(self.caseLabel)
        self.examBox.addWidget(self.caseexaminerList)
        self.mainBox.addLayout(self.examBox)
        self.setLayout(self.mainBox)

    def addExaminer(self):
        tmpexaminer, ok = QInputDialog.getText(self, "Add New Examiner", "Enter Examiner's Official Name:")
        if ok and tmpexaminer:
            self.data.sql(self.data.insertExaminer, [tmpexaminer], 0)
        self.updateExaminerList()

    def addCaseExaminer(self):
        currentexaminerid = self.data.sql(self.data.getCurrentExaminerId, [self.examinerList.currentItem().text()], 1)
        self.data.sql(self.data.addCaseExaminer, [self.parent.currentcaseid, currentexaminerid], 0)
        self.updateCaseExaminerList()

    def delCaseExaminer(self):
        currentexaminerid = self.data.sql(self.data.getCurrentExaminerId, [self.caseexaminerList.currentItem().text()], 1)
        self.data.sql(self.data.removeCaseExaminer, [self.parent.currentcaseid, currentexaminerid], 0)
        self.updateCaseExaminerList()

    def examinerDown(self):
        currentexaminerid = self.data.sql(self.data.getCurrentExaminerId, [self.caseexaminerList.currentItem().text()], 1)
        self.data.sql(self.data.downCaseExaminer, [self.parent.currentcaseid, currentexaminerid], 0)
        self.updateCaseExaminerList()

    def examinerUp(self):
        currentexaminerid = self.data.sql(self.data.getCurrentExaminerId, [self.caseexaminerList.currentItem().text()], 1)
        self.data.sql(self.data.upCaseExaminer, [self.parent.currentcaseid, currentexaminerid], 0)
        self.updateCaseExaminerList()

    def updateActions(self):
        if self.sender() is self.examinerList:
            self.addCaseExaminerAction.setEnabled(True)
            for tmpaction in [self.examinerDownAction, self.examinerUpAction, self.delCaseExaminerAction]:
                tmpaction.setEnabled(False)
            if self.caseexaminerList.currentItem() is not None:
                self.caseexaminerList.currentItem().setSelected(False)
        elif self.sender() is self.caseexaminerList:
            self.addCaseExaminerAction.setEnabled(False)
            for tmpaction in [self.examinerDownAction, self.examinerUpAction, self.delCaseExaminerAction]:
                tmpaction.setEnabled(True)
            if (self.caseexaminerList.indexFromItem(self.caseexaminerList.currentItem()).row() + 1) == self.caseexaminerList.count():
                self.examinerDownAction.setEnabled(False)
            if self.caseexaminerList.indexFromItem(self.caseexaminerList.currentItem()).row() == 0:
                self.examinerUpAction.setEnabled(False)
            if self.examinerList.currentItem() is not None:
                self.examinerList.currentItem().setSelected(False)
        else:
            self.common.displayEroor("Invalid List", "Invalid List Action. If this error continues, please file a bug report.", "warning")

    def updateLists(self):
        self.updateCaseExaminerList()
        self.updateExaminerList()

    def updateExaminerList(self):
        self.examinerList.clear()
        self.examinerList.addItems(self.data.sql(self.data.updateExaminerList, None, 1))
        self.addCaseExaminerAction.setEnabled(False)

    def updateCaseExaminerList(self):
        self.caseexaminerList.clear()
        self.caseexaminerList.addItems(self.data.sql(self.data.updateCaseExaminerList, [self.parent.currentcaseid], 1))
        for tmpaction in [self.examinerDownAction, self.examinerUpAction, self.delCaseExaminerAction]:
            tmpaction.setEnabled(False)
